package cn.ljy.uem.service.impl;


import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolTeachingTaskCommentQuery;
import cn.ljy.uem.service.SchoolTeachingTaskCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * @author 廖振豪
 * @description 教学章节资源评论模块的业务层接口
 * @date 2022-7-18 15:22
 */
@Service
@Slf4j
public class ISchoolTeachingTaskCommentService extends IBaseService<SchoolTeachingTaskComment, SchoolTeachingTaskCommentQuery> implements SchoolTeachingTaskCommentService {

    @Autowired
    private SchoolTeachingTaskCommentMapper mapper;

    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    @Autowired
    private SchoolTeachingTaskMapper schoolTeachingTaskMapper;

    @Override
    protected void beforeGet(Serializable id) {

    }

    @Override
    protected void beforeUpdate(SchoolTeachingTaskComment schoolTeachingTaskComment) throws MyWebException {
        if (mapper.findById(schoolTeachingTaskComment.getId()) == null) {
            throw new MyWebException("编辑失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeSave(SchoolTeachingTaskComment comment) {

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }

    @Override
    protected void afterGet(SchoolTeachingTaskComment comment) {

        //添加教师关联属性
        if (comment.getTeacherId() != null) {
            //查找教师信息
            //赋值
            comment.setSchoolTeacher(schoolTeacherMapper.findById(comment.getTeacherId()));
        }
        //添加学生关联属性
        if (comment.getTeacherId() != null) {
            //查找学生信息
            //赋值
            comment.setSchoolStudent(schoolStudentMapper.findById(comment.getStudentId()));
        }
        //教学班关联属性
        if (comment.getTaskId() != null) {
            //教学班信息
            //赋值
            comment.setSchoolTeachingTask(schoolTeachingTaskMapper.findById(comment.getTaskId()));
        }
    }

    @Override
    protected void afterFindAll(List<SchoolTeachingTaskComment> list) {
        for (SchoolTeachingTaskComment comment : list) {

            //添加教师关联属性
            if (comment.getTeacherId() != null) {
                //查找教师信息
                //赋值
                comment.setSchoolTeacher(schoolTeacherMapper.findById(comment.getTeacherId()));
            }
            //添加学生关联属性
            if (comment.getTeacherId() != null) {
                //查找学生信息
                //赋值
                comment.setSchoolStudent(schoolStudentMapper.findById(comment.getStudentId()));
            }
            //教学班关联属性
            if (comment.getTaskId() != null) {
                //教学班信息
                //赋值
                comment.setSchoolTeachingTask(schoolTeachingTaskMapper.findById(comment.getTaskId()));
            }
        }
    }

}
